//
//  loginViewController.m
//  lmq
//
//  Created by 千翼网络科技 on 2017/1/17.
//  Copyright © 2017年 yuanxin. All rights reserved.
//

#import "loginViewController.h"
#import "registViewController.h"
#import "UIView+Common.h"
#import "userViewController.h"
#import "forgetViewController.h"
#import "homeViewController.h"
#import "AppDelegate.h"
#import "MyTabBarController.h"
#import "Masonry.h"

@interface loginViewController ()

@end

@implementation loginViewController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTitle:@"登录"];
//    [self leftNavHide];


    self.view.backgroundColor=[ZJCModel colorWithString:@"f2f2f2"];
    
    UIImageView *mainImageView=[[UIImageView alloc]initWithFrame:CGRectMake((DEF_SCREEN_WIDTH-(180))/2, (30), (180), (60))];
    mainImageView.image=[UIImage imageNamed:@"lemaiquan"];
    [self.view addSubview:mainImageView];
    
    UIView *userView=[[UIView alloc]initWithFrame:CGRectMake((DEF_SCREEN_WIDTH-(300))/2, (120), (300), (45))];
    userView.layer.borderWidth=(1.0);
    [self.view addSubview:userView];
    
    userTextField=[[UITextField alloc]initWithFrame:CGRectMake((20), (10), (260), (25))];
    userTextField.placeholder=@"请输入账号";
    userTextField.keyboardType=UIKeyboardTypeDefault;
//    userTextField.font = [UIFont systemFontOfSize:14];
    userTextField.clearButtonMode=UITextFieldViewModeWhileEditing;
    [userView addSubview:userTextField];
    
    UIView *passwordView=[[UIView alloc]initWithFrame:CGRectMake((DEF_SCREEN_WIDTH-(300))/2, (120+16+45), (300), (45))];
    passwordView.layer.borderWidth=(1.0);
    [self.view addSubview:passwordView];
    
    passwordField=[[UITextField alloc]initWithFrame:CGRectMake((20), (10), (260), (25))];
    passwordField.placeholder=@"请输入密码";
    passwordField.keyboardType=UIKeyboardTypeDefault;
    passwordField.clearButtonMode=UITextFieldViewModeWhileEditing;
    passwordField.secureTextEntry=YES;
    [passwordView addSubview:passwordField];
    
    UIButton * registButton=[[UIButton alloc]initWithFrame:CGRectMake(passwordView.left, (120+16+90+15), (110), (12))];
    //    registButton.backgroundColor=[UIColor redColor];
    [registButton setTitle:@"注册新用户" forState:UIControlStateNormal];
    [registButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    registButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    //    registButton.backgroundColor=[UIColor redColor];
    [registButton addTarget:self action:@selector(registButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:registButton];
    
    UIButton *forgetButton=[[UIButton alloc]initWithFrame:CGRectMake(DEF_SCREEN_WIDTH-passwordView.left-(100), (120+16+90+15), (100), (12))];
    //    forgetButton.backgroundColor=[UIColor blueColor];
    [forgetButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [forgetButton setTitle:@"忘记密码" forState:UIControlStateNormal];
    forgetButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [forgetButton addTarget:self action:@selector(forgetButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:forgetButton];
    
    UIButton *loginButton=[[UIButton alloc]initWithFrame:CGRectMake(passwordView.left, (120+16+90+15+12+23),(300), (45))];
    [loginButton setTitle:@"登录" forState:UIControlStateNormal];
    loginButton.backgroundColor=[UIColor blackColor];
    [loginButton addTarget:self action:@selector(loginButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:loginButton];
    
    UIButton *buttonOfQX = [[UIButton alloc] init];
    [self.view addSubview:buttonOfQX];
    [buttonOfQX mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(loginButton.mas_bottom).with.offset(20);
        make.centerX.equalTo(self.view);
        make.height.offset(30);
        
    }];
    [buttonOfQX addTarget:self action:@selector(leftNavItemClick) forControlEvents:UIControlEventTouchUpInside];
    [buttonOfQX setTitle:@"随便看看" forState:UIControlStateNormal];
    [buttonOfQX setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    
    //  添加手势
    UITapGestureRecognizer *tapGesure = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(restoreKeyBoard)];
    [self.view addGestureRecognizer:tapGesure];
    
//    UIView *xView=[[UIView alloc]initWithFrame:CGRectMake(0, DEF_SCREEN_HEIGHT-49-64-(1), DEF_SCREEN_WIDTH, (1))];
//    xView.backgroundColor=[UIColor grayColor];
//    [self.view addSubview:xView];
}

-(void)registButtonClick{
    NSLog(@"注册新用户");
    registViewController *registVC=[[registViewController alloc]init];
    [self.navigationController pushViewController:registVC animated:YES];
}

-(void)forgetButtonClick{
    NSLog(@"忘记密码");
    forgetViewController *forgetVC=[[forgetViewController alloc]init];
    [self.navigationController pushViewController:forgetVC animated:YES];
}

-(void)loginButtonClick{

    
        NSMutableDictionary *loginParams=[[NSMutableDictionary alloc]init];
        [loginParams setValue:userTextField.text forKey:@"name"];
        [loginParams setValue:passwordField.text forKey:@"password"];
    
        [[MNetworkHandler sharedInstance]conURL:Login_URL
                                    networkType:NetWorkPOST
                                         params:loginParams
                                       delegate:nil
                                        showHUD:NO
                                   successBlock:^(id returnData) {
                                            if ([[returnData objectForKey:@"code"]intValue]==1) {
                                                
//                                                [self.navigationController popToRootViewControllerAnimated:YES];
                                                
                                                MyTabBarController *barController = [[MyTabBarController alloc] init];
                                                [self presentViewController:barController animated:YES completion:nil];
                                                
                                                
//                                                [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:5.0f];
                                                [PublicFunction saveConfig:userTextField.text forKey:@"phone"];
                                                NSDictionary *dict= [returnData objectForKey:@"data"];
                                                [PublicFunction saveConfig:[dict objectForKey:@"token"] forKey:@"token"];
                                                
                                                [PublicFunction saveConfig:[dict objectForKey:@"uid"] forKey:@"uid"];
                                               
                                                
                                                NSLog(@"QQQQQQQQQQQQQQQQQQ%@", returnData);
                                                NSLog(@"5555555555555555%@", dict);
                                                
                                                
                                       }
                                            else if ([[returnData objectForKey:@"code"]intValue]==-2){
                                           [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:5.0f];
                                       }
                                   } failureBlock:^(NSError *error) {
                                       NSLog(@"%@",error);
                                       [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
                                   }];
    
    
}

#pragma mark 手势隐藏键盘
- (void)restoreKeyBoard
{
    [userTextField resignFirstResponder];
    [passwordField resignFirstResponder];
}

- (void)leftNavItemClick{

    [self.tabBarController setSelectedIndex:0];
    [self.navigationController popToRootViewControllerAnimated:NO];


}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
